package com.tanhua.server.controller;

import com.tanhua.common.vo.ErrorResult;
import com.tanhua.server.service.QXVideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class VideoController {

    @Autowired
    private QXVideoService QXVideoService;

    /**
     * 查询小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> findVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = QXVideoService.findVideoList(page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发布小视频
     *
     * @param
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> sendVideo(@RequestParam("videoThumbnail") MultipartFile cover,
                                          @RequestParam("videoFile") MultipartFile video) {
        try {
            Boolean result = QXVideoService.sendVideo(cover, video);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 小视频的评论列表
     *
     * @param videoId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("{id}/comments")
    public ResponseEntity<PageResult> findVideoList(@PathVariable("id") String videoId,
                                                    @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = QXVideoService.findCommentList(videoId, page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @PostMapping("{id}/comments")
    public ResponseEntity<Object> sendComment(@PathVariable("id") String videoId,
                                            @RequestBody Map<String, String> param) {
        try {
            String comment = param.get("comment");
            Boolean result = QXVideoService.sendComment(videoId, comment);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 小视频点赞
     *
     * @return
     */
    @PostMapping("{id}/like")
    public ResponseEntity<Object> likeVideo(@PathVariable("id") String videoId) {
        try {
            Long result = QXVideoService.likeVideo(videoId);
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 小视频取消点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("{id}/dislike")
    public ResponseEntity<Object> disLikeVideo(@PathVariable("id") String videoId) {
        try {
            Long result = QXVideoService.disLikeVideo(videoId);
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 小视频评论点赞
     *
     * @param commentId
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity<Object> likeVideoComment(@PathVariable("id") String commentId) {
        try {
            Long result = QXVideoService.likeVideo(commentId);
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 小视频评论取消点赞
     *
     * @param commentId
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity<Object> disLikeVideoComment(@PathVariable("id") String commentId) {
        try {
            Long result = QXVideoService.disLikeVideo(commentId);
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 小视频关注功能
     * @param userId 发布这条视频的用户的ID
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity<Object> focusVideo(@PathVariable("uid") Long userId) {
        try {
            Boolean result = QXVideoService.focusVideo(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
         ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 取消关注小视频
     * @param userId
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity<Object> unFocusVideo(@PathVariable("uid") Long userId){
        try {
            Boolean result = QXVideoService.unFocusVideo(userId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("账号封禁中,无效操作!").build();
        //如果返回值为空,则说明验证失败,返回状态码500并将对应错误信息传递给用户
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
